<?php
namespace app\admin\controller;

use app\admin\model\Article as ArticleModel;
use app\admin\controller\Base;

class Article extends Base
{
    public function lit()
    {
        $list = ArticleModel::paginate(3);
        $this->assign('list', $list);
        return $this->fetch('lit');
    }
    public function add()
    {
        if (request()->isPost()) {
            $data = [
                'title' => input('title'),
                'author' => input('author'),
                'desc' => input('desc'),
                'keywords' => input('keywords'),
                'content' => input('content'),
                'time' => time(),
                'cateid' => input('cateid'),
            ];
            if (input('state') == 'on') {
                $data['state'] = 1;
            } else {
                $data['state'] = 0;
            }
            // 获取上传的图片
            $file = request()->file('pic');
            if ($file) {
                $info = $file->move(ROOT_PATH . 'public/static' . DS . 'uploads');
                $data['pic'] = $info->getSaveName();
            }
            // 检测上传的数据
            $validate = validate('Article');
            $check = $validate->check($data);
            if (!$check) {
                $this->Error($validate->getError());
                die;
            }
            $result = db('article')->insert($data);
            if ($result) {
                $this->success('添加成功', 'lit');
            } else {
                $this->error('添加不成功');
            }
        };
        $cate = db('cate')->select();
        $this->assign('cateList', $cate);
        return $this->fetch('add');
    }
    public function edit()
    {
        $id = input('id');
        $articles = db('article')->find($id);
        if (request()->isPost()) {
            $data = [
                'title' => input('title'),
                'author' => input('author'),
                'desc' => input('desc'),
                'keywords' => input('keywords'),
                'content' => input('content'),
                'time' => time(),
                'cateid' => input('cateid'),
            ];
            if (input('state') == 'on') {
                $data['state'] = 1;
            } else {
                $data['state'] = 0;
            }
            // 获取上传的图片
            $file = request()->file('pic');
            if ($file) {
                $info = $file->move(ROOT_PATH . 'public/static' . DS . 'uploads');
                $data['pic'] = $info->getSaveName();
            }
            $result = db('article')->where('id', $id)->update($data);
            if ($result) {
                $this->success('更改成功', 'lit');
            } else {
                $this->error('更改不成功');
            }

        } else {
            $this->assign('articles', $articles);
            $cates = db('cate')->select();
            $this->assign('cateList', $cates);
            return $this->fetch('edit');
        }

    }
    public function del()
    {
        $result = db('article')->where('id', input('id'))->delete();
        if ($result) {
            $this->success('删除成功', 'lit');
        } else {
            $this->success('删除失败', 'lit');
        }
    }
}
